﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MrytPub
{
    public class PaintPanel : UserControl
    {
        Bitmap bmp = null;
        Graphics g_bmp = null;

        private bool CanMove = false;
        private Point startPoint;

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // PaintPanel
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Name = "PaintPanel";
            this.Size = new System.Drawing.Size(653, 449);
            this.ResumeLayout(false);

        }

        public PaintPanel(Bitmap bmp)
        {
            this.bmp = bmp;
            g_bmp = Graphics.FromImage(bmp);
            InitializeComponent();
            this.Dock = DockStyle.Fill;
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.AllPaintingInWmPaint, true);
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            this.CanMove = true;
            this.startPoint = new Point(e.X, e.Y);
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (this.CanMove)
            {
                g_bmp.DrawLine(new Pen(Color.Red, 3), this.startPoint, new Point(e.X, e.Y));
                this.startPoint.X = e.X;
                this.startPoint.Y = e.Y;
                this.Refresh();
            }
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            this.CanMove = false;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            if (g == null) return;
            if (g_bmp != null)
            {
                g.DrawImage((Image)bmp, 0, 0);
            }
        }
    }
}
